var plays = play;
var shareState=false;
//1,进入游戏说明界面
$(".start .startMenu").click(function () {
    // var nowTime=new Date().getTime();
    // alert(nowTime+"---"+parseInt(globalVar.startTimeStatus)+"---"+parseInt(globalVar.endTimeStatus));
    // if(nowTime>=parseInt(globalVar.startTimeStatus)&&nowTime<=parseInt(globalVar.endTimeStatus)){
        $(".enter").hide();
        $(".rule").show();
    // }else{
        // $("body .total").show();
        // $(".total .info").html("抱歉，活动已结束!");
    // }

    //开始游戏
    clickListener(globalVar.source, "h1-100", "开始游戏按钮");
});
//2，进入游戏准备界面角
$(".rule .knowBtn").click(function () {
    
    //游戏规则说明
    clickListener(globalVar.source, "h1-101", "游戏规则说明");
    $(".rule").hide();
    $("body .ready").show();
    $("body .musicBox").show();
    var audio = document.getElementById('music');  
    if (audio.paused) {
        audio.play();// 播放 
    }
    var num = 5;
    var timer = setInterval(function () {
        num--;
        if (num <= 0) {
            //3，进入游戏界面
            clearInterval(timer);
            $("body .ready").hide();
            $("body .game").show();
            playGame(parseInt(globalVar.remainPlayNumber));
        } else {
            var str = 'http://cdn.yhq.lenovo.com.cn/school/img/daojishi' + num + '.png';
            $(".ready .daojishi").attr("src", str);
        }

    }, 1000);
});

//4，游戏函数界面
function playGame(count) {
    if (count <= 0) {
        $("body .total").show();
    } else {
        plays();
    }
}
//5,游戏结果页
//5.1领年货
var prize = $('.result .maxCount').html();
// //var prize ="联想电脑";

console.log(prize);
// //发礼物
sendPrize(prize);

//5.2分享后抽奖学金
$("body .getLuck").click(function () {
     //再接好运
     clickListener(globalVar.source, "h1-103", "再接好运");
    if (globalVar.attenStatus == '0') {
        $("body .follow").show();
        //根据sorce变更二维码
        checkAttention(globalVar.source);
    } else {
        $("body .studyMoney").hide();
        $("body .luckyBag").hide();
        $("body .share").show();
        shareState=true;
    }

})
//微信分享;
initWechatShare();
wx.ready(function () {
    wx.hideMenuItems({
        menuList: ['menuItem:share:qq', 'menuItem:share:QZone', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari'] // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
    });
    //微信分享
    var title = "你来，才算“接”大欢喜！";
    var image ="http://cdn.yhq.lenovo.com.cn/school/img/icon.jpg";
    var description = "老铁，点一下，我来接你啦~ 联想电脑返校季，一起接好礼";
    var link = "";
    var cfg = {
        title: title,
        desc: description,
        link: link || location.href,
        imgUrl: image || config.defaultImage,
        success: function () {
            $("body .share").hide();
            //分享设置
            var shareId = unescape(getParameter("shareId"));
            shareRecord(1, link || location.href, shareId);
            //抽红包
            if (shareState) {
                getMoney("share");
            }
        }
    };
    var cfgApp = {
        title: title,
        desc: description,
        link: link || location.href,
        imgUrl: image || config.defaultImage,
        success: function () {
            $("body .share").hide();
            //分享设置
            var shareId = unescape(getParameter("shareId"));
            shareRecord(0, link || location.href, shareId);
            //抽红包
            if (shareState) {
                getMoney("share");
            }

        }
    }
    wx.onMenuShareTimeline(cfg);
    wx.onMenuShareAppMessage(cfgApp);
});
//5.3发礼物函数
function sendPrize(key) {
    if (key == '联想电脑') {
        $(".resultBtn .drawGift").click(function () {
            //会员卡领取
            clickListener(globalVar.source, "h1-102", "会员卡领取");
            if (globalVar.attenStatus == '0') {
                $("body .follow").show();
                //根据sorce变更二维码
                checkAttention(globalVar.source);
            } else {
                getMember();
                
            }
        });
        // $("body .MemberCard").show();
        $(".result .titleTip").html("接货一分钟<br>红包抽到手");

    } else if (key == '福袋') {

        var tipWord = ["福袋接的多<br>考试不挂科", "福气接满<br>科科过关", "开学大吉<br>今晚吃鸡", "有福临门<br>游戏超神", "有福在手<br>要啥都有"];
        var k = parseInt(Math.random() * 5);
        $(".result .titleTip").html(tipWord[k]);

        $(".giftTip").html("福星高照，好运来到~");

        $(".resultBtn .drawGift").click(function () {
            //会员卡领取
            clickListener(globalVar.source, "h1-102", "会员卡领取");
            if (globalVar.attenStatus == '0') {
                $("body .follow").show();
                //根据sorce变更二维码
                checkAttention(globalVar.source);
            } else {
                if(globalVar.inputStatus == '0'){
                    $("body .phone").show();
                    $("body").addClass("body-fixed");
                }else{
                    getMember();
                }
                
            }
        });
    }
}
//6,其他功能
//6.1点击x关闭浮层
$(".natant .shutDown").click(function () {
    $('body .natantBox').hide();
    //获取关注状态
    getUserInfo();
    $("body").removeClass("body-fixed");
})

//手机号浮层，点提交功能
$("body .submit").click(function () {
    $(".errorTip").hide();
    //手机号录入
    clickListener(globalVar.source, "h1-105", "手机号录入");
    checkInfo(); //手机号，图形验证码，手机验证码校验
    var phoneInfo = checkInfo(); //接受手机号和手机验证码
    $.ajax({
        url: globalVar.path + "/v1/lenovo/userinfo/submitPhone.do",
        dataType: "jsonp",
        type: "post",
        data: phoneInfo,
        success: function (data) {
            if (data.result == "ok") {
                globalVar.inputStatus = data.values.inputStatus; //更改用户录入状态，已录入
                localStorage.setItem('user', JSON.stringify(globalVar)); //更改本地存储状态
                getMember(); //绑定会员卡
            } else {
                $(".errorTip").show();
                $(".errorTipWord").html(data.errormsg);
            }
        }
    });
})
//绑定会员卡函数
function getMember() {
    $.ajax({
        url: globalVar.path + "/v1/lenovo/member/binding.do",
        dataType: "jsonp",
        type: "post",
        data: {
            "source": globalVar.source
        },
        success: function (data) {
            if (data.result == "ok") {
                window.location.href = "https://i.lenovo.com.cn/memberInfo/center.jhtml?sts=6cd57e4e-1cc5-437a-b09a-d71a16a7ddd4";
            } else {
                window.location.href = "https://i.lenovo.com.cn/memberInfo/center.jhtml?sts=6cd57e4e-1cc5-437a-b09a-d71a16a7ddd4";
            }
        },
        error: function (data) {}
    });
}
//6.5抽红包接口函数
function getMoney(type) {

    if (type == "share") {
        shareState = false;
    }
    //调抽红包接口
    if (globalVar.redDrawStatus == 0) {
        $.ajax({
            url: globalVar.path + "/v1/lenovo/redPacket/draw.do",
            dataType: "jsonp",
            type: "post",
            data: {
                drawType: 1
            },
            success: function (data) {
                if (data.result == "ok") {
                    rbStatuss(data.values.status, data.values.redAmount);
                } else {
                    $("body .studyMoney").show(); //结果层
                }
            },
            error: function (data) {
            }
        });
    } else {
        $("body .studyMoney").show(); //结果层
    }
}
//红包中奖状态函数
function rbStatuss(state, count) {
    if (state == 0) { //未中奖
        $("body .studyMoney").show(); //结果层
    } else {
        globalVar.redDrawStatus = '1';
        $("body  .studyMoney").show(); //结果层
        $(".studyMoney  .yesBag").show();
        $(".studyMoney  .noBag").hide(); //结果层
        //中奖金额
        var str = parseInt(count) / 100;
        //抽奖后，修改页面金额
        globalVar.newRedAmount = count;
        localStorage.setItem('user', JSON.stringify(globalVar));
        //读取,更新
        globalVar = JSON.parse(localStorage.getItem("user"));

        $(".studyMoney .moneyCount").html(str); //有中奖信息
        //调发红包接口
        $.ajax({
            url: globalVar.path + "/v1/lenovo/redPacket/sendRed.do",
            dataType: "jsonp",
            type: "post",
            success: function (data) {
                if (data.result == "error") { //有错
                } else { //无错
                }
            },
            error: function (data) {
            }
        });
    }
}

/** 关闭手机号录入浮层 */
$("body .alertClose").click(function () {
    $(".errorTip").hide();
});
/** 学生专享 */
$(".moreBtn .student").click(function () {

     //学生专享
     clickListener(globalVar.source, "h1-106", "学生专享");
     window.location.href = "https://campus.alipay-eco.com/public/card/card.html#/lenovoCard/index?channel=fxwxh5";
});
/** 附近店铺 */
$(".moreBtn .coupon").click(function () {
    //附近店铺
    clickListener(globalVar.source, "h1-107", "附近店铺");
    //链接跳转
    window.location.href = "http://jyhshop.lenovo.com.cn/product/lenovo_shop/map/wgs.html";
});
/** 官方商城 */
$(".moreBtn .product").click(function () {
    //官方商城
    clickListener(globalVar.source, "h1-108", "官方商城");
    //链接跳转
    window.location.href =checkUrl(globalVar.source);
});

/** 获取用户关注状态 */
function getUserInfo() {
    $.ajax({
        url: globalVar.path + "/v1/lenovo/lucky2018/validateUser.do",
        type: "post",
        dataType: "jsonp",
        data: {
            shareId: globalVar.shareId
        },
        success: function (data) {
            if (data.result == "ok") {
                if (data.values.attenStatus == "1") {
                    globalVar.attenStatus = data.values.attenStatus;
                    localStorage.setItem('user', JSON.stringify(globalVar));
                }
            } else {
                return;
            }
        },
        error: function (data) {
        }
    });
}

$(".replay").click(function(){
    window.location.reload();
})
$("img").click(function(e){
	e.preventDefault()
});

/**积分抽奖 */
$("body .infoBtnImg").click(function(){
    //积分抽奖
    clickListener(globalVar.source, "h1-104", "积分抽奖");
    var total=parseInt($(".resultInfo .sum").html());
    if(total>=88){
        if (globalVar.attenStatus == '0') {
            $("body .follow").show();
            //根据sorce变更二维码
            checkAttention(globalVar.source);
        } else {
                //抽红包
                getMoney("draw");
        }
    }else{
        $("body .total").show();
        $(".total .info").html("您本次游戏获得积分未达到88分，不能参与抽奖。");
    }
});
/**音乐播放暂停 */
$(".musicBox .musicIcon").click(function(){
   
    var audio = document.getElementById('music');  
    if (audio.paused) {
        audio.play();// 播放 
    }else {
        audio.pause(); // 停止
    } 
})